<template>
  <common-info-dialog
    :visible.sync="dialogShow"
    @cancel="close"
    :title="dialogTitle"
  >
    <common-info-table>
      <common-info-table-cell label="告警规则类别" width="50%"
        >{{
          ruleTypes.find((item) => item.paramValue === detail.ruleType) &&
          ruleTypes.find((item) => item.paramValue === detail.ruleType)
            .paramName
        }}
      </common-info-table-cell>
      <common-info-table-cell label="规则名称" width="50%"
        >{{ detail.ruleName }}
      </common-info-table-cell>
      <common-info-table-cell label="规则编码" width="50%"
        >{{ detail.ruleCode }}
      </common-info-table-cell>
      <common-info-table-cell label="告警码" width="50%"
        >{{ detail.alarmCode }}
      </common-info-table-cell>
      <common-info-table-cell label="告警类型" width="50%"
        >{{ detail.alarmType }}
      </common-info-table-cell>
      <!-- <common-info-table-cell label="告警子类型" width="50%"
        >{{ detail.alarmBusiness }}
      </common-info-table-cell> -->
      <common-info-table-cell label="告警等级" width="50%"
        >{{
          alarmLevelList.find((item) => +item.id === detail.degree) &&
          alarmLevelList.find((item) => +item.id === detail.degree).name
        }}
      </common-info-table-cell>
      <common-info-table-cell label="级别提升基数" width="50%"
        >{{ detail.alertDegreeUp }}
      </common-info-table-cell>
      <common-info-table-cell label="杀伤链" width="50%"
        >{{
          killChainList.find((item) => item.paramValue === detail.killChain) &&
          killChainList.find((item) => item.paramValue === detail.killChain)
            .paramName
        }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType !== 'multiSequenceAlert' && ruleType !== 'analyseAlert'"
        label="索引类型"
        width="50%"
        >{{
          indexTypes.find(
            (item) => item.paramValue === detail.ruleDetails.dataSourceId
          ) &&
          indexTypes.find(
            (item) => item.paramValue === detail.ruleDetails.dataSourceId
          ).paramName
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType !== 'multiSequenceAlert' && ruleType !== 'analyseAlert'"
        label="告警合并字段"
        :width="ruleType === 'singleAlert' ? '100%' : '50%'"
      >
        {{ mergeFieldString }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType !== 'singleAlert' && ruleType !== 'analyseAlert'"
        label="监控频率"
        width="50%"
      >
        {{ detail.ruleDetails.frequency }}
        {{
          unitOptions.find(
            (item) => item.value === detail.ruleDetails.frequencyType
          ) &&
          unitOptions.find(
            (item) => item.value === detail.ruleDetails.frequencyType
          ).label
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType !== 'singleAlert' && ruleType !== 'analyseAlert'"
        label="统计时间范围"
        :width="
          ruleType === 'singleSequenceAlert' ||
          ruleType === 'multiSequenceAlert'
            ? '100%'
            : '50%'
        "
        >{{ detail.ruleDetails.timesInterval }}
        {{
          unitOptions.find(
            (item) => item.value === detail.ruleDetails.intervalType
          ) &&
          unitOptions.find(
            (item) => item.value === detail.ruleDetails.intervalType
          ).label
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'limitAlert'"
        label="对比时间范围"
        width="50%"
        >前 {{ detail.ruleDetails.limitInterval }} 天
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'countAlert' || ruleType === 'limitAlert'"
        label="统计方式"
        width="50%"
        >{{
          statsTypeList.find(
            (item) => item.value === detail.ruleDetails.statsType
          ) &&
          statsTypeList.find(
            (item) => item.value === detail.ruleDetails.statsType
          ).label
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="
          ruleType === 'countAlert' && detail.ruleDetails.statsType !== 'count'
        "
        label="指标字段"
        width="50%"
        >{{
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.aggField
          ) &&
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.aggField
          ).columnAlias
        }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType === 'limitAlert'"
        label="对比字段"
        width="50%"
        >{{
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.limitField
          ) &&
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.limitField
          ).columnAlias
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'limitAlert'"
        label="条件1"
        width="50%"
        >{{
          conditionOperList.find(
            (item) => item.paramValue === detail.ruleDetails.rule1Type
          ) &&
          conditionOperList.find(
            (item) => item.paramValue === detail.ruleDetails.rule1Type
          ).paramName
        }}
        {{ detail.ruleDetails.rule1Value }} %
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'limitAlert'"
        label="条件2"
        width="50%"
        >{{
          conditionOperList.find(
            (item) => item.paramValue === detail.ruleDetails.rule2Type
          ) &&
          conditionOperList.find(
            (item) => item.paramValue === detail.ruleDetails.rule2Type
          ).paramName
        }}
        {{ detail.ruleDetails.rule2Value }} %
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'countAlert'"
        label="阈值"
        :width="detail.ruleDetails.statsType !== 'count' ? '50%' : '100%'"
        >{{ detail.ruleDetails.threshold }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'singleSequenceAlert'"
        label="时序字段值"
        width="100%"
      >
        <div
          v-for="(item, index) in detail.ruleDetails.referenceFields"
          :key="index"
        >
          {{
            optionList.find(
              (item) =>
                item.columnName ===
                detail.ruleDetails.referenceFields[index].name
            ) &&
            optionList.find(
              (item) =>
                item.columnName ===
                detail.ruleDetails.referenceFields[index].name
            ).columnAlias
          }}：
          {{ detail.ruleDetails.referenceFields[index].value }}
        </div>
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'multiAlert'"
        label="日志条数"
        width="50%"
        >{{ detail.ruleDetails.logNum }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'multiAlert'"
        label="不同维度"
        width="100%"
        >{{
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.dimensionDiff
          ) &&
          optionList.find(
            (item) => item.columnName === detail.ruleDetails.dimensionDiff
          ).columnAlias
        }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="索引类型1"
        width="100%"
        >{{
          indexTypes.find(
            (item) =>
              item.paramValue ===
              detail.ruleDetails.alertConditions[0].dataSourceId
          ) &&
          indexTypes.find(
            (item) =>
              item.paramValue ===
              detail.ruleDetails.alertConditions[0].dataSourceId
          ).paramName
        }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="关联字段1"
        width="100%"
        >{{ associationField1String }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="过滤规则1"
        width="100%"
        >{{ alertConditionString1 }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="索引类型2"
        width="100%"
        >{{
          indexTypes.find(
            (item) =>
              item.paramValue ===
              detail.ruleDetails.alertConditions[1].dataSourceId
          ) &&
          indexTypes.find(
            (item) =>
              item.paramValue ===
              detail.ruleDetails.alertConditions[1].dataSourceId
          ).paramName
        }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="关联字段2"
        width="100%"
        >{{ associationField2String }}
      </common-info-table-cell>
      <common-info-table-cell
        v-if="ruleType === 'multiSequenceAlert'"
        label="过滤规则2"
        width="100%"
        >{{ alertConditionString2 }}
      </common-info-table-cell>

      <common-info-table-cell
        v-if="ruleType !== 'multiSequenceAlert' && ruleType !== 'analyseAlert'"
        label="过滤规则"
        width="100%"
        >{{ conditionString }}
      </common-info-table-cell>

      <common-info-table-cell label="备注" width="100%"
        >{{ detail.remark }}
      </common-info-table-cell>
    </common-info-table>
  </common-info-dialog>
</template>

<script>
import { mapState } from "vuex";
import { UNITOPTIONS, STATISTICALS, CONDITIONOPERATOR } from "@/utils/constant";
import {
  getRuleCondition,
  generateSqlString,
  generateTuple,
  replaceOperator,
} from "@/utils/util";

export default {
  props: {
    loginPolicySelect: {
      type: Array,
      default: () => [],
    },
    authSelect: {
      type: Array,
      default: () => [],
    },
    typeStaticOption: {
      type: Array,
      default: () => [],
    },
    typeStaticListOption: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      dialogTitle: "用户详情",
      dialogShow: false,
      detail: {
        ruleDetails: {},
      },
      conditionString: "",
      alertConditionString1: "",
      alertConditionString2: "",
      ruleType: "singleAlert",
      unitOptions: UNITOPTIONS,
      statsTypeList: STATISTICALS, //事件数量 统计方式列表
      conditionOperaOptions: CONDITIONOPERATOR, //条件1\条件2符号列表
      mergeFieldString: "",
      associationField1String: "",
      associationField2String: "",
      optionList: [],
    };
  },
  computed: {
    ...mapState("rule", [
      "ruleTypes",
      "alarmLevelList",
      "alertTypeList",
      "killChainList",
      "indexTypes",
      "conditionOperList",
      "typeSearchList",
      "typeStaticList",
      "typeShowList",
      "typeShowLessList",
    ]),
    ...mapState("log", ["operatorList"]),
  },
  methods: {
    /**
     * 关闭
     */
    close() {
      this.dialogShow = false;
    },
    /**
     * 显示
     */
    show(data) {
      this.dialogTitle = "详情";
      this.detail = data.alertRuleId ? data : {};
      this.conditionString = getRuleCondition(this.detail.ruleDetails);

      this.detail.ruleDetails = JSON.parse(this.detail.ruleDetails);
      this.ruleType = this.detail.ruleType;

      if (
        this.detail.ruleDetails.alertConditions &&
        this.detail.ruleDetails.alertConditions[0]
      ) {
        this.alertConditionString1 = replaceOperator(
          generateSqlString(
            generateTuple(this.detail.ruleDetails.alertConditions[0].condition)
          )
        );
      }

      if (
        this.detail.ruleDetails.alertConditions &&
        this.detail.ruleDetails.alertConditions[1]
      ) {
        this.alertConditionString2 = replaceOperator(
          generateSqlString(
            generateTuple(this.detail.ruleDetails.alertConditions[1].condition)
          )
        );
      }

      this.optionList =
        this.detail.ruleDetails.dataSourceId === "index-bwda-ark-apisecurity"
          ? this.typeStaticOption
          : this.typeStaticListOption;
      //告警合并字段
      if (this.detail.ruleDetails.mergeField) {
        let mergeFieldArray = [];

        let optionList =
          this.detail.ruleDetails.dataSourceId === "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;

        this.detail.ruleDetails.mergeField.forEach((item) => {
          optionList.forEach((type) => {
            if (type.columnName === item) {
              mergeFieldArray.push(type.columnAlias);
            }
          });
        });

        this.mergeFieldString = mergeFieldArray.join(" ");
      }

      //关联字段1
      if (
        this.detail.ruleDetails.alertConditions &&
        this.detail.ruleDetails.alertConditions[0]
      ) {
        let associationField1Arr = [];

        let optionList =
          this.detail.ruleDetails.alertConditions[0].dataSourceId ===
          "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;

        this.detail.ruleDetails.alertConditions[0].associationField.forEach(
          (item) => {
            optionList.forEach((type) => {
              if (type.columnName === item) {
                associationField1Arr.push(type.columnAlias);
              }
            });
          }
        );

        this.associationField1String = associationField1Arr.join(" ");
      }

      //关联字段2
      if (
        this.detail.ruleDetails.alertConditions &&
        this.detail.ruleDetails.alertConditions[1]
      ) {
        let associationField2Arr = [];
        let optionList =
          this.detail.ruleDetails.alertConditions[1].dataSourceId ===
          "index-bwda-ark-apisecurity"
            ? this.typeStaticOption
            : this.typeStaticListOption;

        this.detail.ruleDetails.alertConditions[1].associationField.forEach(
          (item) => {
            optionList.forEach((type) => {
              if (type.columnName === item) {
                associationField2Arr.push(type.columnAlias);
              }
            });
          }
        );

        this.associationField2String = associationField2Arr.join(" ");
      }

      this.dialogShow = true;
    },
  },
};
</script>
